package protocol_http;

import protocol.ServerProtocol;
import tokenizer_http.HttpMessage;
import tokenizer_http.PostMessage;

public class HttpProtocol implements ServerProtocol {
	
	String[] splittedMessage;
	
	public Object processMessage(Object httpMessage) {
		splitMessage((HttpMessage)httpMessage);
		if(!splittedMessage[0].matches("((((^POST))+ /+((login)|(list)|(create_group)|(send)|(add_user)|(remove_user)))|(((^GET))+ /+((logout)|(queue))))+.+(jps)+( HTTP/1.1)")
				| !((HttpMessage) httpMessage).getMessage().contains("\n\n"))
			return new HttpMessage("HTTP/1.1 200" + "/n" + "404" +"/n" + "/n"+ "/n" + "404 - Not Found" + "/n"+ "$");
		return null;
	}
	

	@Override
	public boolean isEnd(Object msg) {
		if(((String)msg).contains("$"))
			return true;
		return false;
	}
	
	public void splitMessage(HttpMessage message) {
		splittedMessage = (message.getMessage()).split("\n");
	}

}

